/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife updater.

    Ages of Strife updater is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife updater is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife updater.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class NewsItem {
	private Translations translations;
	private Connection connection;

	private String[] title;
	private String[] text;
	private boolean[] translate;
	private boolean[] translateTitle;
	
	private String[] titleCache;
	private String[] newsCache;
	private String[] languageCache;
	
	public NewsItem( Connection connection, Translations translations ) {
		this.connection = connection;
		this.translations = translations;
		text = new String[0];
		translate = new boolean[0];
		title = new String[0];
		translateTitle = new boolean[0];
		titleCache = new String[0];
		newsCache = new String[0];
		languageCache = new String[0]; 
	}
	
	public void addText( String textToAdd, boolean translate ) {
		String[] tmpText = new String[text.length + 1];
		boolean[] tmpTranslate = new boolean[text.length + 1];
		
		for( int i = 0; i < text.length; i ++ ) {
			tmpText[i] = text[i];
			tmpTranslate[i] = this.translate[i];
		}
		tmpText[text.length] = textToAdd;
		tmpTranslate[text.length] = translate;
		
		text = tmpText;
		this.translate = tmpTranslate;
	}
	
	public void addTitleText( String textToAdd, boolean translate ) {
		String[] tmpTitle = new String[title.length + 1];
		boolean[] tmpTranslateTitle = new boolean[title.length + 1];
		
		for( int i = 0; i < title.length; i ++ ) {
			tmpTitle[i] = title[i];
			tmpTranslateTitle[i] = translateTitle[i];
		}
		tmpTitle[title.length] = textToAdd;
		tmpTranslateTitle[title.length] = translate;
		
		title = tmpTitle;
		translateTitle = tmpTranslateTitle;
	}
	
	public void insert( int rulerId, int newsType, long turn, String date ) throws NoSuchAlgorithmException, SQLException {
		PreparedStatement preparedStatement = connection.prepareStatement( "SELECT language " +
				"FROM users " +
				"JOIN ruler ON users.user_id = ruler.user_id " +
				"WHERE ruler_id = ?" );
		preparedStatement.setInt( 1, rulerId );
		
		ResultSet resultSet = preparedStatement.executeQuery();
		// Check if we can even find this users language
		// It could just be that we can't cause he resigned, 
		// in that case we don't need to insert news either
		if( resultSet.next() ) {
			String language = resultSet.getString( 1 );
			String news;
			String subject;
			
			// See if we already have this news item translated		
			int cacheHit = -1;
			for( int i = 0; i < languageCache.length; i ++ ) {
				if( languageCache[i] == language ) {
					cacheHit = i;
					break;
				}
			}
			
			if( cacheHit != -1 ) {
				news = newsCache[cacheHit];
				subject = titleCache[cacheHit];
			}
			else {
				StringBuffer translated = new StringBuffer();
				for( int i = 0; i < text.length; i ++ ) {
					if( translate[i] ) {
						translated.append( translations.translate( text[i], language ) );
					}
					else {
						translated.append( text[i] );
					}
				}
				
				StringBuffer translatedTitle = new StringBuffer();
				for( int i = 0; i < title.length; i ++ ) {
					if( translateTitle[i] ) {
						translatedTitle.append( translations.translate( title[i], language ) );
					}
					else {
						translatedTitle.append( title[i] );
					}
				}
				
				// Add this news item translation to the cache
				String[] tmpLanguageCache = new String[languageCache.length + 1];
				String[] tmpNewsCache = new String[newsCache.length + 1];
				String[] tmpTitleCache = new String[titleCache.length + 1];
				
				for( int i = 0; i < languageCache.length; i ++ ) {
					tmpLanguageCache[i] = languageCache[i];
					tmpNewsCache[i] = newsCache[i];
					tmpTitleCache[i] = titleCache[i];
				}
				subject = tmpTitleCache[titleCache.length] = translatedTitle.toString();
				news = tmpNewsCache[newsCache.length] = translated.toString();
				tmpLanguageCache[languageCache.length] = language;
				
				languageCache = tmpLanguageCache;
				newsCache = tmpNewsCache;
				titleCache = tmpTitleCache;
			}
			
			preparedStatement = connection.prepareStatement( "INSERT INTO news " +
					"( ruler_id, type, subject, news, date, turn ) " +
					"VALUES( ?, ?, ?, ?, ?, ? )" );
			preparedStatement.setInt( 1, rulerId );
			preparedStatement.setInt( 2, newsType );
			preparedStatement.setString( 3, subject );
			preparedStatement.setString( 4, news );
			preparedStatement.setString( 5, date );
			preparedStatement.setLong( 6, turn );
			preparedStatement.execute();
		}
	}
	
	public void clear() {
		text = new String[0];
		translate = new boolean[0];
		title = new String[0];
		translateTitle = new boolean[0];
		titleCache = new String[0];
		newsCache = new String[0];
		languageCache = new String[0]; 	
	}
}
